package com.lsj.threadtest;

import java.util.ArrayList;
import java.util.List;

/**
 * 如果一个类继承Thread，则不适合资源共享。但是如果实现了Runable接口的话，则很容易的实现资源共享。
 */
public class MyThread2 implements Runnable{
    private int key;
    private String mark;
    private List<String> names;

    public MyThread2(List<String> names,String mark,int key){
        this.names = names;
        this.mark = mark;
        this.key = key;
    }
    public void run(){
       for(int i=0;i<names.size();i++){
//           if(i==names.size()-1){
//               for(int j=0;j<names.size();j++){
//                   System.out.println(names.get(j));
//               }
//           }
           if(i%key==0){
               names.set(i,names.get(i)+mark);
               System.out.println(names.get(i));
           }

       }
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        for(int i=0;i<10;i++){
            list.add("name"+i);
        }
        new Thread(new MyThread2(list,"->a",3)).start();
        new Thread(new MyThread2(list,"->b",2)).start();

    }
}

